package cn.burnit.view;

import cn.burnit.pojo.User;
import cn.burnit.service.UserService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 修改密码
 */
public class UpdatePwdForm extends JFrame {


    private JPanel panel0,panel1,panel2,panel3,panel4;
    private JLabel title,usernamelab,passwordlab,newPasswordlab;
    private JTextField usernameField;
    private JPasswordField passwordField,newPasswordField;
    private JButton submit,cancel;

    public UpdatePwdForm() {

        Container container = getContentPane();
        container.setLayout(null);
        title=new JLabel("图书管理系统");
        title.setFont(new Font("宋体", Font.PLAIN, 24));
        usernamelab=new JLabel("请输入用户名:");
        passwordlab=new JLabel("请输入新密码:");
        newPasswordlab=new JLabel("再次确认密码:");
        submit=new JButton("确定");
        cancel=new JButton("取消");
        cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updatePassword(e);
            }
        });

        usernameField=new JTextField();
        usernameField.setColumns(15);
        passwordField=new JPasswordField();
        newPasswordField=new JPasswordField();
        passwordField.setEchoChar('*');
        passwordField.setColumns(15);
        newPasswordField.setEchoChar('*');
        newPasswordField.setColumns(15);
        panel0=new JPanel();
        panel1=new JPanel();
        panel2=new JPanel();
        panel3=new JPanel();
        panel3.setLayout(new FlowLayout(FlowLayout.CENTER));
        panel4=new JPanel();

        panel0.add(title);
        panel4.add(usernamelab);
        panel4.add(usernameField);
        panel1.add(passwordlab);
        panel1.add(passwordField);
        panel2.add(newPasswordlab);
        panel2.add(newPasswordField);
        panel3.add(submit);
        panel3.add(cancel);

        container.add(panel0,BorderLayout.NORTH);
        container.add(panel4);
        container.add(panel1);
        container.add(panel2);

        container.add(panel3);

        setTitle("修改密码");
        setLayout(new FlowLayout(FlowLayout.CENTER));
        setSize(350,300);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        //setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void updatePassword(ActionEvent e) {
        String username = usernameField.getText();
        String password = new String(passwordField.getPassword());
        String newPassword = new String(newPasswordField.getPassword());
        UserService userService = new UserService();
        User user = new User(username, password);
        if (password.equals(newPassword)){
            //User user=userService.findUserByUsername(username);

            int i=userService.updatePwd(user);
            if (i>0){
                JOptionPane.showMessageDialog(null,"修改成功");
                dispose();
            }else {
                JOptionPane.showMessageDialog(null,"修改失败");
            }

        }else {
            JOptionPane.showMessageDialog(null,"两次密码不一致");
        }

    }

//    public static void main(String[] args) {
//        new UpdatePwdForm();
//    }
}
